package net.caixiaomi.info.ui.discovery;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.interfaces.MatchingSelectCallBack;
import net.caixiaomi.info.ui.main.MatchFragment;
import net.caixiaomi.info.ui.matching.MatchItemFragment;
import net.caixiaomi.info.ui.view.MatchingSelectView;
import net.caixiaomi.info.ui.view.SelectScrollView;
import net.caixiaomi.info.widgets.DefLoading;
import net.caixiaomi.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by mac on 2018/9/5.
 */

public class ActiveCenterActivity extends BaseActivity implements ViewPager.OnPageChangeListener,MatchingSelectCallBack {

    private MyFragmentPagerAdapter adapter;

    @BindView(R.id.matching_select_view)
    SelectScrollView mSelectView;

    @BindView(R.id.scroll_view)
    HorizontalScrollView scroll_view;

    @BindView(R.id.fragment_view_pager)
    ViewPager fragment_view_pager;

    private Context mContext;
    public int mType=0;

    private ActiveFragment mActingFragment;
    private ActiveFragment mEndFragment;
    private ArrayList<ActiveFragment> mFragmentList;

    @Override
    protected int layout() {
        return R.layout.active_center_layout;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
        mLoading = new DefLoading(mContext);

        //view pager
        adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),
                getFragmentList()
        );
        fragment_view_pager.setAdapter(adapter);
        fragment_view_pager.addOnPageChangeListener(this);
        fragment_view_pager.setOffscreenPageLimit(3);

        mSelectView.setCallBack(this);

//        setType(mType);
    }

    /**
     * 生成碎片列表
     */
    private ArrayList<ActiveFragment> getFragmentList() {
        mFragmentList = new ArrayList<>();
        mActingFragment=ActiveFragment.newInstance();
        mActingFragment.setType(0);
        mFragmentList.add(mActingFragment);
        mEndFragment=ActiveFragment.newInstance();
        mEndFragment.setType(1);
        mFragmentList.add(mEndFragment);
        return mFragmentList;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mType=position;
        mSelectView.setPageSelected(position);

        smoothScrollTo(position * mSelectView.subTitleWidth);
//        mFragmentList.get(position).setRequestData(mCurrentTime,mLeagueId,String.valueOf(position),true);

    }

    /**
     * 流畅滑动
     */
    private void smoothScrollTo(final int scrollTo) {
        final int currentPosition = scroll_view.getScrollX();
        Animation anim = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                int scrollOffset = scrollTo - currentPosition;
                scroll_view.scrollTo(currentPosition + (int) (scrollOffset * interpolatedTime), 0);
            }
        };
        anim.setDuration(300);
        scroll_view.startAnimation(anim);
    }


    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void matchingBarSelected(int position) {
        if(mType==position){
//            loadData(mType,true);
        }
        fragment_view_pager.setCurrentItem(position);
    }

    /**
     * viewpager adapter
     */
    private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        private ArrayList<ActiveFragment> list;

        public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<ActiveFragment> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public Fragment getItem(int arg0) {
            return list.get(arg0);
        }

        @Override
        public int getCount() {
            return list.size();
        }

    }

//    private void setType(int position){
//        mFragmentList.get(position).setType(String.valueOf(position));
//    }
}
